package cn.edu.ctbu.datajpa01.webapi;


import cn.edu.ctbu.datajpa01.core.PageUtils;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {

    @Autowired
    private StudentService studentService;

    /**
     * 读取的url：/webapi/student/list
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students=studentService.findAll();
        return students;
    }
    /**
     * 分页 读取全部内容
     * @return
     * */
    @GetMapping("/getbypage")
    public PageUtils getByPage(
            @RequestParam(value = "page",defaultValue = "0") Integer page,
            @RequestParam(value = "size",defaultValue = "10") Integer size,
            @RequestParam(value = "name",defaultValue = "" ) String name,
            @RequestParam(value = "no",defaultValue = "") String no){
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        Page<Student> studentPage = null;
        if(!StringUtils.isEmpty(name)){
            Student student = new Student();
            student.setName(name);
            Pageable pageable= PageRequest.of(0,10,sort);
            //全部模糊查询 即%{name}%
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example= Example.of(student,matcher);
            studentPage = studentService.findAll(example,pageable);
        }else if(!StringUtils.isEmpty(no)) {
            Student student = new Student();
            student.setNo(no);
            Pageable pageable= PageRequest.of(0,10,sort);
            //全部模糊查询 即%{no}%
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Student> example= Example.of(student,matcher);
            studentPage = studentService.findAll(example,pageable);
        }else{
            Pageable pageable = PageRequest.of(page,size,sort);
            studentPage = studentService.findAll(pageable);
        }
        PageUtils pageUtils = new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return  pageUtils;
    }


    /**
     * 读取的url:/webqpi/student/name
     * @param name
     * @return
     */
    @GetMapping("/name")
    public List<Student> findByName(String name){
        List<Student> students=studentService.findByName(name);
        return students;
    }

    /**
     * 新增学生的方法/webapi/student/insert
     * @param student
     * @return
     */
    @PostMapping("/insert")
    public Student insert(Student student){
        Student student1=studentService.insert(student);
        return student1;
    }
    /**
     * 更新的方法
     * @param student
     * @return
     */
    @PutMapping("/update")
    public Student update(Student student){
        //读取旧的数据
        Student oddstudent=studentService.findById(student.getId());
        if(StringUtils.isEmpty(student.getNo())){
            student.setNo(oddstudent.getNo());
        }
        student=studentService.update(student);
        return student;
    }
    /**
     * 删除学生的方法/webapi/student/delete
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public void delete2(@PathVariable Integer id){
        studentService.delete2(id);
    }
    /**
     * 读取的url：/webapi/student/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
        Student student=studentService.findById(id);
        student.setPassword("");
        return student;
    }


}
